package com.gae.blore.client.layer;

import java.util.ArrayList;
import java.util.List;

import org.gwtopenmaps.openlayers.client.Map;
import org.gwtopenmaps.openlayers.client.Style;
import org.gwtopenmaps.openlayers.client.feature.VectorFeature;
import org.gwtopenmaps.openlayers.client.geometry.Point;
import org.gwtopenmaps.openlayers.client.layer.Vector;
import org.gwtopenmaps.openlayers.client.layer.VectorOptions;
import org.gwtopenmaps.openlayers.client.util.Attributes;

import com.gae.blore.client.domain.BusStop;
import com.gae.blore.client.domain.FireBrigadeStation;
import com.gae.blore.client.domain.Hospital;
import com.gae.blore.client.domain.MetroStation;
import com.gae.blore.client.domain.PoliceStation;
import com.gae.blore.client.domain.RailwayStation;
import com.google.gwt.user.client.Window;

public class FeatureLayer {

	private Vector featureLayer;
	private String layerName = "Benagaluru Data";
	private Map map;
	private List<VectorFeature> busStopFeatures;
	private List<VectorFeature> hospFeatures;
	private List<VectorFeature> polFeatures;
	private List<VectorFeature> fireFeatures;
	private List<VectorFeature> metroFeatures;
	private List<VectorFeature> railFeatures;

	public FeatureLayer(Map map) {
		this.map = map;

		// Create the style for each purpose
		/*Style styleNormal = createStyle("images/Bus.jpg");
		Style styleSelected = createSelectStyle();
		Style styleHighlighted = styleSelected;

		// Create the StyleMap to handle all styles
		StyleMap styleMapVector = new StyleMap(styleNormal, styleSelected,
				styleHighlighted);
*/
		// Create the vectorOptions to use the styles
		VectorOptions vectorOptions = new VectorOptions();
		//vectorOptions.setStyleMap(styleMapVector);
		vectorOptions.setLayerOpacity(1.0);
		vectorOptions.setIsBaseLayer(false);
		vectorOptions.setProjection("EPSG:900913");
		featureLayer = new Vector(layerName, vectorOptions);

		map.addLayer(featureLayer);

	}

	private Style createStyle(String imageUrl) {
		Style style = new Style();

		style.setExternalGraphic(imageUrl);
		// style.setFillColor(fillColor);
		// style.setFillOpacity(0.5);
		style.setPointRadius(10);
		style.setGraphicZIndex(2000);
		style.setFillOpacity(1.0);
		// style.setStrokeOpacity(1.0);

		return style;

	}

	/*private Style createSelectStyle() {
		Style style = new Style();
		style.setFillOpacity(1.0);
		return style;

	}

	private SelectFeatureOptions createSelectFeatureOptions(boolean isHover) {
		// Create the SelectFeature and its Options
		SelectFeatureOptions selectFeatureOptions = new SelectFeatureOptions();

		if (isHover == true) {
			selectFeatureOptions.setHover();
			selectFeatureOptions.setHighlightOnly(true);
			selectFeatureOptions.setRenderIntent(RenderIntent.TEMPORARY);
		}

		// Add Select event
		selectFeatureOptions.onSelect(new SelectFeatureListener() {
			public void onFeatureSelected(VectorFeature vectorFeature) {
				Window.alert("Selected");
				
				 * BusDetailsDialog busDetailsDialog = new BusDetailsDialog(
				 * vectorFeature.getAttributes()); busDetailsDialog.show();
				 
				System.out.println("Selected" + vectorFeature);

			}
		});

		// Add Unselect event
		selectFeatureOptions.onUnSelect(new UnselectFeatureListener() {
			public void onFeatureUnselected(VectorFeature vectorFeature) {
				Window.alert("UnSelected");
				System.out.println("UnSelected" + vectorFeature);

			}
		});

		return selectFeatureOptions;
	}
*/
	public VectorFeature getFeature(String id) {
		return featureLayer.getFeatureById(id);
	}

	public void addBusStopsToMap(List<BusStop> busStops) {

		busStopFeatures = new ArrayList<VectorFeature>();

		for (BusStop busStop : busStops) {
			
			Point point = new Point(busStop.getLng(), busStop.getLat());
			VectorFeature neFeature = new VectorFeature(point);
			Style style= createStyle("images/bus.jpg");
			neFeature.setStyle(style);
			Attributes attributes = neFeature.getAttributes();
			attributes.setAttribute("type", "BusStop");
			attributes.setAttribute("id", busStop.getId());
			attributes.setAttribute("name", busStop.getName());
			// neFeature.getAttributes().setAttribute("customLabel",
			// networkElement.getName());
			neFeature.getJSObject().setProperty("id", busStop.getId());
			busStopFeatures.add(neFeature);
		}

		// create a point feature

		featureLayer.addFeatures(busStopFeatures
				.toArray(new VectorFeature[busStopFeatures.size()]));

	}

	public void addPoliceStationsToMap(List<PoliceStation> policeStations) {
		try {

			polFeatures = new ArrayList<VectorFeature>();

			for (PoliceStation policeStation : policeStations) {
				Point point = new Point(policeStation.getLng(),
						policeStation.getLat());
				VectorFeature polFeature = new VectorFeature(point);
				Style style= createStyle("images/police.jpg");
				polFeature.setStyle(style);
				Attributes attributes = polFeature.getAttributes();
				attributes.setAttribute("type", "PoliceStation");
				attributes.setAttribute("id", policeStation.getId());
				attributes.setAttribute("name", policeStation.getName());
				// neFeature.getAttributes().setAttribute("customLabel",
				// networkElement.getName());
				polFeature.getJSObject().setProperty("id",
						"PoliceStation" + "-" + policeStation.getId());
				polFeatures.add(polFeature);
			}

			featureLayer.addFeatures(polFeatures
					.toArray(new VectorFeature[polFeatures.size()]));
		} catch (Exception exception) {
			Window.alert(exception.toString());

		}
	}

	public void addHospitalsToMap(List<Hospital> hospitals) {
		try {
			hospFeatures = new ArrayList<VectorFeature>();

			for (Hospital hospital : hospitals) {
				Point point = new Point(hospital.getLng(), hospital.getLat());

				VectorFeature hospFeature = new VectorFeature(point);
				Style style= createStyle("images/hosp.jpg");
				hospFeature.setStyle(style);
				Attributes attributes = hospFeature.getAttributes();
				attributes.setAttribute("type", "Hospital");
				attributes.setAttribute("id",
						hospital.getId());
				attributes.setAttribute("name", hospital.getName());
				// neFeature.getAttributes().setAttribute("customLabel",
				// networkElement.getName());
				hospFeature.getJSObject().setProperty("id",
						"Hospital" + "-" + hospital.getId());

				hospFeatures.add(hospFeature);

			}

			// create a point feature

			featureLayer.addFeatures(hospFeatures
					.toArray(new VectorFeature[hospFeatures.size()]));
		} catch (Exception exception) {
			Window.alert(exception.toString());

		}

	}

	public void addFireBrigadeStationToMap(
			List<FireBrigadeStation> brigadeStations) {

		fireFeatures = new ArrayList<VectorFeature>();

		for (FireBrigadeStation brigadeStation : brigadeStations) {
			Point point = new Point(brigadeStation.getLng(),
					brigadeStation.getLat());

			VectorFeature fireFeature = new VectorFeature(point);
			Style style= createStyle("images/fire.jpg");
			fireFeature.setStyle(style);
			Attributes attributes = fireFeature.getAttributes();
			attributes.setAttribute("type", "FireBrigade");
			attributes.setAttribute("id",
					brigadeStation.getId());
			attributes.setAttribute("name", brigadeStation.getName());
			fireFeature.getJSObject().setProperty("id",
					"FireBrigade" + "-" + brigadeStation.getId());

			fireFeatures.add(fireFeature);
		}

		// create a point feature

		featureLayer.addFeatures(fireFeatures
				.toArray(new VectorFeature[fireFeatures.size()]));

	}

	public void addRailwayStationToMap(List<RailwayStation> railwayStations) {

		railFeatures = new ArrayList<VectorFeature>();

		for (RailwayStation railwayStation : railwayStations) {
			Point point = new Point(railwayStation.getLng(),
					railwayStation.getLat());

			VectorFeature railFeature = new VectorFeature(point);
			Style style= createStyle("images/rail.jpg");
			railFeature.setStyle(style);
			Attributes attributes = railFeature.getAttributes();
			attributes.setAttribute("type", "RailwayStation");
			attributes.setAttribute("id", railwayStation.getId());
			attributes.setAttribute("name", railwayStation.getName());
			// neFeature.getAttributes().setAttribute("customLabel",
			// networkElement.getName());
			railFeature.getJSObject().setProperty("id",
					"RailwayStation" + "-" + railwayStation.getId());

			railFeatures.add(railFeature);
		}

		// create a point feature

		featureLayer.addFeatures(railFeatures
				.toArray(new VectorFeature[railFeatures.size()]));

	}

	public void addMetroStationToMap(List<MetroStation> metroStations) {

		metroFeatures = new ArrayList<VectorFeature>();

		for (MetroStation metroStation : metroStations) {
			Point point = new Point(metroStation.getLng(),
					metroStation.getLat());

			VectorFeature railFeature = new VectorFeature(point);
			Style style= createStyle("images/metro.jpg");
			railFeature.setStyle(style);
			Attributes attributes = railFeature.getAttributes();
			attributes.setAttribute("type", "MetroStation");
			attributes.setAttribute("id",
					metroStation.getId());
			attributes.setAttribute("name", metroStation.getName());
			// neFeature.getAttributes().setAttribute("customLabel",
			// networkElement.getName());
			railFeature.getJSObject().setProperty("id",
					"MetroStation" + "-" + metroStation.getId());

			metroFeatures.add(railFeature);
		}

		// create a point feature

		featureLayer.addFeatures(metroFeatures
				.toArray(new VectorFeature[metroFeatures.size()]));

	}

	public void clearBusStopsFromMap() {

		if (null != busStopFeatures) {
			clearFeatures(busStopFeatures);
			busStopFeatures = null;
		}
	}

	public void clearHospitalsFromMap() {
		if (null != hospFeatures) {
			clearFeatures(hospFeatures);
			hospFeatures = null;

		}
	}

	public void clearPoliceStations() {
		if (null != polFeatures) {
			clearFeatures(polFeatures);
			polFeatures = null;
		}
	}

	public void clearFireBrigadeStations() {
		if (null != fireFeatures) {
			clearFeatures(fireFeatures);
			fireFeatures = null;
		}
	}

	public void clearMetroStations() {
		if (null != metroFeatures) {
			clearFeatures(metroFeatures);
			metroFeatures = null;
		}
	}

	public void clearRailStations() {
		if (null != railFeatures) {
			clearFeatures(railFeatures);
			railFeatures = null;
		}
	}

	private void clearFeatures(List<VectorFeature> features) {
		for (VectorFeature feature : features) {
			featureLayer.removeFeature(feature);
		}

	}

}
